﻿#include<format>
#include<ranges>
#include<vector>
#include"./CfgParseTools.hpp"
#include"../../Third/yaml/include/yaml-cpp/yaml.h"
#include"../../Base/Framework/Device.hpp"


namespace MMF
{

    std::map<std::string, std::string> CfgParseTools::ParseKV(const std::string& strKV)
    {
        //返回值
        std::map<std::string, std::string> ret;

        //对逗号进行拆分
        for (const auto& word: std::views::split(strKV, ','))
        {
            //对kv进行拆分
            auto kvView = word | std::views::split('=') | std::views::transform([](const auto& item){return std::string(item.begin(), item.end());});
            std::vector<std::string> kv(kvView.begin(), kvView.end());

            //插入键值对
            ret.emplace(kv[0], kv[1]);
        }

        return ret;
    }

    std::string CfgParseTools::PackKV(const std::map<std::string, std::string>& kv)
    {
        std::string ret;
        for (const auto& pair : kv)
        {
            ret += pair.first;
            ret += '=';
            ret += pair.second;
            ret += ',';
        }

        ret.resize(ret.length() - 1);
        return ret;
    }

    std::vector<MMFDevice> CfgParseTools::ParseDevice(YAML::Node& deviceNode)
    {
        std::vector<MMFDevice> ret;

        for (YAML::Node::iterator it = deviceNode.begin(); it != deviceNode.end(); it++)
        {
            MMFDevice dev = {};
            dev.mName = it->first.as<std::string>();
            dev.mLibrayFile = it->second["LibaryFile"].as<std::string>();
            dev.mCreateFuncName = it->second["CreateFunc"].as<std::string>();
            dev.mExtendInfo = it->second["ExtendInfo"].as<std::string>();
            dev.mIsSingle = it->second["IsSingle"].as<bool>();

            std::vector<int> inTypes = it->second["InTypes"].as<std::vector<int>>();
            std::vector<int> outTypes = it->second["OutTypes"].as<std::vector<int>>();

            for (const int& val : inTypes)
                dev.mInType.push_back((MMFDataType)val);

            for (const int& val : outTypes)
                dev.mOutType.push_back((MMFDataType)val);
            
            ret.push_back(dev);
        }

        return ret;
    }

    std::vector<MMFServerCpt> CfgParseTools::ParseServerComponent(YAML::Node& deviceNode)
    {
        std::vector<MMFServerCpt> ret;

        for (YAML::Node::iterator it = deviceNode.begin(); it != deviceNode.end(); it++)
        {
            MMFServerCpt cpt = {};
            cpt.mName = it->first.as<std::string>();
            cpt.mLibrayFile = it->second["LibaryFile"].as<std::string>();
            cpt.mCreateFuncName = it->second["CreateFunc"].as<std::string>();
            cpt.mExtendInfo = it->second["ExtendInfo"].as<std::string>();
            cpt.mIsExcutable = false;

            ret.push_back(cpt);
        }

        return ret;
    }

    std::vector<MMFUnitCpt> CfgParseTools::ParseUnitComponent(YAML::Node& deviceNode)
    {
        std::vector<MMFUnitCpt> ret;

        for (YAML::Node::iterator it = deviceNode.begin(); it != deviceNode.end(); it++)
        {
            MMFUnitCpt cpt = {};
            cpt.mName = it->first.as<std::string>();
            cpt.mLibrayFile = it->second["LibaryFile"].as<std::string>();
            cpt.mCreateFuncName = it->second["CreateFunc"].as<std::string>();
            cpt.mExtendInfo = it->second["ExtendInfo"].as<std::string>();
            cpt.mIsExcutable = it->second["IsExcutable"].as<bool>();

            ret.push_back(cpt);
        }

        return ret;
    }
}